<template>
  <div class="article-comments">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check="false"
    >
      <comment-item
        v-for="(item, index) in list"
        :key="index"
        :item="item"
        @click-reply="clickReply"
      />
    </van-list>
  </div>
</template>

<script>
import { getComments } from '@/api/comment'
import CommentItem from './comment-item.vue'

export default {
  name: "article-comment",
  components: {
    CommentItem
  },
  data() {
    return {
      loading: false,
      finished: false,
      offset: null
    }
  },
  props: {
    artId: {
      type: [String, Number]
    },
    list: {
      type: Array,
      default: () => []
    },
    type: {
      type: String,
      default: 'a',
      validator: function(value) {
        return ['a', 'c'].includes(value)
      }
    }
  },
  created () {
    this.loading = true
    this.onLoad()
  },
  methods: {
    async onLoad() {
      try {
        let params = {
          type: this.type,
          source: this.artId,
          offset: this.offset,
          limit: 10
        }

        let {data: { data: { results, total_count, last_id } } } = await getComments(params)

        this.list.push(...results)
        this.offset = last_id
        this.loading = false
        this.$emit('update-count', total_count)

        if( this.list.length >= total_count ) {
          this.finished = true
        }
      } catch (err) {
        console.log(err)
      }
    },
    clickReply (item) {
      this.$emit('click-reply2', item)
    }
  }
}
</script>

<style scoped lang="less">
  .publish-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }

  .van-list {
    margin-bottom: 45px;
  }
</style>
